<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot与Caffeine缓存集成指南</title>
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f8f9fa;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            margin-top: 1.5em;
            margin-bottom: 0.5em;
            color: #2d3748;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 5rem 0;
        }
        .card {
            transition: all 0.3s ease;
            border-radius: 0.5rem;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .code-block {
            background-color: #282c34;
            color: #abb2bf;
            border-radius: 0.375rem;
            padding: 1rem;
            font-family: 'Courier New', Courier, monospace;
            overflow-x: auto;
        }
        .icon-box {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 1rem;
        }
        .annotation-icon {
            background-color: rgba(102, 126, 234, 0.1);
            color: #667eea;
        }
        .cache-icon {
            background-color: rgba(118, 75, 162, 0.1);
            color: #764ba2;
        }
        .feature-icon {
            background-color: rgba(56, 182, 255, 0.1);
            color: #38b6ff;
        }
        .step-icon {
            background-color: rgba(16, 185, 129, 0.1);
            color: #10b981;
        }
        .highlight {
            background: linear-gradient(120deg, rgba(102, 126, 234, 0.2) 0%, rgba(102, 126, 234, 0) 100%);
            border-left: 4px solid #667eea;
            padding: 0.5rem 1rem;
            margin: 1rem 0;
        }
        .mermaid-container {
            background-color: white;
            padding: 1.5rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            margin: 2rem 0;
        }
        footer {
            background-color: #1a202c;
            color: #e2e8f0;
        }
        .nav-link {
            position: relative;
        }
        .nav-link::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 0;
            height: 2px;
            background-color: #667eea;
            transition: width 0.3s ease;
        }
        .nav-link:hover::after {
            width: 100%;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="bg-white shadow-sm sticky top-0 z-50">
        <div class="container mx-auto px-6 py-4 flex justify-between items-center">
            <a href="#" class="text-2xl font-bold text-indigo-600 flex items-center">
                <i class="fas fa-bolt mr-2"></i> 技术小馆
            </a>
            <div class="hidden md:flex space-x-8">
                <a href="#intro" class="nav-link text-gray-600 hover:text-indigo-600">简介</a>
                <a href="#features" class="nav-link text-gray-600 hover:text-indigo-600">特性</a>
                <a href="#integration" class="nav-link text-gray-600 hover:text-indigo-600">集成</a>
                <a href="#annotations" class="nav-link text-gray-600 hover:text-indigo-600">注解</a>
                <a href="#case" class="nav-link text-gray-600 hover:text-indigo-600">案例</a>
            </div>
        </div>
    </nav>

    <!-- Hero 区域 -->
    <section class="hero">
        <div class="container mx-auto px-6 py-16 text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-4">Spring Boot与Caffeine缓存集成指南</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">高性能Java缓存解决方案，提升微服务应用性能</p>
            <div class="max-w-3xl mx-auto">
                <div class="bg-white bg-opacity-20 p-6 rounded-lg backdrop-blur-sm">
                    <p class="text-lg md:text-xl mb-4">"在现代的微服务架构中，缓存已经成为提升系统性能、降低数据库压力和提高响应速度的关键技术之一。"</p>
                </div>
            </div>
        </div>
    </section>

    <!-- 主要内容区域 -->
    <main class="container mx-auto px-6 py-12">
        <!-- 简介部分 -->
        <section id="intro" class="mb-20">
            <div class="flex flex-col md:flex-row items-center mb-12">
                <div class="md:w-1/2 mb-8 md:mb-0 md:pr-8">
                    <h2 class="text-3xl font-bold mb-4">什么是Caffeine缓存？</h2>
                    <p class="text-lg text-gray-700 mb-6">Caffeine是一个高性能的Java缓存库，专为现代高并发应用设计。它与Spring Cache完美集成，为Java开发者提供了强大而灵活的缓存解决方案。</p>
                    <div class="flex items-center text-indigo-600">
                        <i class="fas fa-rocket mr-2"></i>
                        <span>极速响应 | 智能回收 | 丰富策略</span>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1740209837762-ba046cc4-329f-42d3-9679-803fc8c88b3d.png" alt="Caffeine缓存性能图表" class="rounded-lg shadow-xl w-full h-auto">
                </div>
            </div>

            <div class="grid md:grid-cols-3 gap-8 mb-12">
                <div class="card bg-white p-6">
                    <div class="icon-box feature-icon mx-auto">
                        <i class="fas fa-tachometer-alt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">高性能</h3>
                    <p class="text-gray-600">Caffeine具有极快的读写性能，适合高并发的场景，显著提升应用响应速度。</p>
                </div>
                <div class="card bg-white p-6">
                    <div class="icon-box feature-icon mx-auto">
                        <i class="fas fa-sliders-h text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">缓存策略</h3>
                    <p class="text-gray-600">支持基于时间、大小等多种缓存失效策略，满足不同业务场景需求。</p>
                </div>
                <div class="card bg-white p-6">
                    <div class="icon-box feature-icon mx-auto">
                        <i class="fas fa-recycle text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">自动回收</h3>
                    <p class="text-gray-600">内存占用达到限制后会自动回收不常用的缓存，防止内存溢出。</p>
                </div>
            </div>

            <div class="highlight">
                <p class="flex items-start">
                    <i class="fas fa-lightbulb mr-2 mt-1 text-yellow-500"></i>
                    <span>相较于其他缓存解决方案（如Guava），Caffeine提供了更高的性能和更多的缓存控制选项，适合在Spring Boot中作为本地缓存解决方案。</span>
                </p>
            </div>
        </section>

        <!-- Spring Cache与Caffeine集成 -->
        <section id="features" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center">Spring Cache与Caffeine集成</h2>
            <div class="flex flex-col md:flex-row items-center mb-12">
                <div class="md:w-1/2 mb-8 md:mb-0">
                    <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1740209789475-cbf094dc-00dd-44ab-971a-af19eb7464e5.png" alt="Spring Cache架构图" class="rounded-lg shadow-xl w-full h-auto">
                </div>
                <div class="md:w-1/2 md:pl-8">
                    <p class="text-lg text-gray-700 mb-6">Spring Cache是Spring Framework提供的一种缓存抽象，简化了缓存操作。通过简单的注解，你可以轻松地对方法进行缓存操作，而不需要手动管理缓存的存取。</p>
                    
                    <h3 class="text-2xl font-semibold mb-4">Caffeine缓存集成的优势</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                            <span><strong>性能提升</strong>：Caffeine缓存对常用数据的快速访问能够大幅提高应用性能，减少数据库负载。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                            <span><strong>内存管理</strong>：Caffeine通过LRU（最近最少使用）策略、最大缓存大小限制、缓存过期等机制自动管理内存，避免内存泄漏。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                            <span><strong>易于集成</strong>：Spring Cache抽象使得与Caffeine的集成非常简单，开发者无需管理缓存的底层实现。</span>
                        </li>
                    </ul>
                </div>
            </div>

            <!-- 数据可视化 -->
            <div class="mermaid-container">
                <div class="mermaid">
                    graph TD
                    A[客户端请求] --> B{缓存命中?}
                    B -->|是| C[从缓存返回数据]
                    B -->|否| D[执行方法]
                    D --> E[将结果存入缓存]
                    E --> F[返回结果]
                    style A fill:#667eea,color:white
                    style B fill:#764ba2,color:white
                    style C fill:#10b981,color:white
                    style D fill:#f6ad55,color:white
                    style E fill:#f6ad55,color:white
                    style F fill:#10b981,color:white
                </div>
            </div>
        </section>

        <!-- 集成步骤 -->
        <section id="integration" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center">集成步骤</h2>
            
            <div class="grid md:grid-cols-3 gap-8 mb-12">
                <div class="card bg-white p-6">
                    <div class="icon-box step-icon mx-auto">
                        <i class="fas fa-code text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">1. 添加依赖</h3>
                    <p class="text-gray-600 mb-4">在pom.xml中添加Caffeine和Spring Cache依赖</p>
                    <div class="code-block text-sm">
                        &lt;dependency&gt;<br>
                        &nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>
                        &nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;<br>
                        &lt;/dependency&gt;<br>
                        &lt;dependency&gt;<br>
                        &nbsp;&nbsp;&lt;groupId&gt;com.github.ben-manes&lt;/groupId&gt;<br>
                        &nbsp;&nbsp;&lt;artifactId&gt;caffeine&lt;/artifactId&gt;<br>
                        &nbsp;&nbsp;&lt;version&gt;2.9.2&lt;/version&gt;<br>
                        &lt;/dependency&gt;
                    </div>
                </div>
                <div class="card bg-white p-6">
                    <div class="icon-box step-icon mx-auto">
                        <i class="fas fa-cog text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">2. 配置Caffeine</h3>
                    <p class="text-gray-600 mb-4">在application.properties中配置缓存策略</p>
                    <div class="code-block text-sm">
                        # 配置Caffeine缓存<br>
                        spring.cache.type=caffeine<br>
                        spring.cache.caffeine.spec=maximumSize=100,expireAfterAccess=600s
                    </div>
                </div>
                <div class="card bg-white p-6">
                    <div class="icon-box step-icon mx-auto">
                        <i class="fas fa-power-off text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">3. 启用缓存</h3>
                    <p class="text-gray-600 mb-4">在主类添加@EnableCaching注解</p>
                    <div class="code-block text-sm">
                        @SpringBootApplication<br>
                        @EnableCaching<br>
                        public class CacheApplication {<br>
                        &nbsp;&nbsp;public static void main(String[] args) {<br>
                        &nbsp;&nbsp;&nbsp;&nbsp;SpringApplication.run(CacheApplication.class, args);<br>
                        &nbsp;&nbsp;}<br>
                        }
                    </div>
                </div>
            </div>
        </section>

        <!-- 缓存注解 -->
        <section id="annotations" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center">缓存注解的使用</h2>
            <p class="text-lg text-gray-700 mb-8 text-center">Spring Cache通过注解简化了缓存操作，以下是常用的注解及其功能</p>
            
            <div class="grid md:grid-cols-3 gap-8 mb-12">
                <div class="card bg-white p-6">
                    <div class="icon-box annotation-icon mx-auto">
                        <i class="fas fa-database text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">@Cacheable</h3>
                    <p class="text-gray-600 mb-4">用于缓存方法返回值。当方法被调用时，Spring会先检查缓存中是否有数据。</p>
                    <div class="code-block text-sm">
                        @Cacheable(value = "users", key = "#id")<br>
                        public User getUserById(Long id) {<br>
                        &nbsp;&nbsp;return findUserById(id);<br>
                        }
                    </div>
                </div>
                <div class="card bg-white p-6">
                    <div class="icon-box annotation-icon mx-auto">
                        <i class="fas fa-trash-alt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">@CacheEvict</h3>
                    <p class="text-gray-600 mb-4">用于清除缓存。常用于当数据发生变更时，清除相应的缓存。</p>
                    <div class="code-block text-sm">
                        @CacheEvict(value = "users", key = "#id")<br>
                        public void updateUser(Long id, String name) {<br>
                        &nbsp;&nbsp;// 更新用户信息<br>
                        }
                    </div>
                </div>
                <div class="card bg-white p-6">
                    <div class="icon-box annotation-icon mx-auto">
                        <i class="fas fa-sync-alt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">@CachePut</h3>
                    <p class="text-gray-600 mb-4">用于更新缓存。与@Cacheable不同，它不检查缓存而是每次执行方法后都会更新缓存。</p>
                    <div class="code-block text-sm">
                        @CachePut(value = "users", key = "#id")<br>
                        public User updateUser(Long id, String name) {<br>
                        &nbsp;&nbsp;return updateUserInDatabase(id, name);<br>
                        }
                    </div>
                </div>
            </div>
        </section>

        <!-- 实战案例 -->
        <section id="case" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center">实战案例：缓存查询结果</h2>
            <p class="text-lg text-gray-700 mb-8">开发一个简单的用户查询系统，使用Caffeine缓存用户查询结果，避免重复查询数据库。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-2xl font-semibold mb-4">1. 定义实体类</h3>
                    <div class="code-block">
                        public class User {<br>
                        &nbsp;&nbsp;private Long id;<br>
                        &nbsp;&nbsp;private String name;<br>
                        &nbsp;&nbsp;private int age;<br>
                        <br>
                        &nbsp;&nbsp;// Getters and Setters<br>
                        }
                    </div>
                </div>
                <div>
                    <h3 class="text-2xl font-semibold mb-4">2. 创建缓存服务</h3>
                    <div class="code-block">
                        @Service<br>
                        public class UserService {<br>
                        <br>
                        &nbsp;&nbsp;@Cacheable(value = "users", key = "#id")<br>
                        &nbsp;&nbsp;public User getUserById(Long id) {<br>
                        &nbsp;&nbsp;&nbsp;&nbsp;return findUserById(id);<br>
                        &nbsp;&nbsp;}<br>
                        <br>
                        &nbsp;&nbsp;private User findUserById(Long id) {<br>
                        &nbsp;&nbsp;&nbsp;&nbsp;// 模拟数据库查询<br>
                        &nbsp;&nbsp;}<br>
                        }
                    </div>
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-2xl font-semibold mb-4">3. 控制器层</h3>
                    <div class="code-block">
                        @RestController<br>
                        public class UserController {<br>
                        <br>
                        &nbsp;&nbsp;@Autowired<br>
                        &nbsp;&nbsp;private UserService userService;<br>
                        <br>
                        &nbsp;&nbsp;@GetMapping("/user/{id}")<br>
                        &nbsp;&nbsp;public User getUserById(@PathVariable Long id) {<br>
                        &nbsp;&nbsp;&nbsp;&nbsp;return userService.getUserById(id);<br>
                        &nbsp;&nbsp;}<br>
                        }
                    </div>
                </div>
                <div>
                    <h3 class="text-2xl font-semibold mb-4">4. 缓存管理与策略</h3>
                    <div class="code-block">
                        # 配置Caffeine缓存策略<br>
                        spring.cache.caffeine.spec=<br>
                        &nbsp;&nbsp;maximumSize=100,<br>
                        &nbsp;&nbsp;expireAfterWrite=3600s,<br>
                        &nbsp;&nbsp;expireAfterAccess=1800s
                    </div>
                    <div class="mt-4 p-4 bg-blue-50 rounded-lg">
                        <p class="flex items-start">
                            <i class="fas fa-info-circle mr-2 mt-1 text-blue-500"></i>
                            <span>当第一次访问<code>/user/{id}</code>时，Spring会查询数据库并将结果存入缓存。第二次访问时，直接从缓存读取，显著提升性能。</span>
                        </p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- 页脚 -->
    <footer class="py-8">
        <div class="container mx-auto px-6 text-center">
            <h3 class="text-xl font-bold mb-2">技术小馆</h3>
            <p class="text-gray-400 mb-4">分享前沿技术，助力开发者成长</p>
            <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition duration-300">
                <i class="fas fa-external-link-alt mr-1"></i> http://www.yuque.com/jtostring
            </a>
        </div>
    </footer>

    <script>
        // 初始化Mermaid图表
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>